

#include <QFile>
#include <QMessageBox>

#include "Relation.h"
#include "RendezVous.h"
#include "NouveauClient.h"
#include "Compatibilite.h"
#include "StatistiquesClient.h"
#include "RechercheCompatibilite.h"

RechercheCompatibilite::RechercheCompatibilite(QWidget *parent)
:QMainWindow(parent), Ui_RechercheCompatibilite()
{
	setupUi(this);
	majListe();
}

void RechercheCompatibilite::majListe()
{
	listWidget->clear();
	InitCompatibilites();
	for(int i=0; i<GetCompatibilites().size(); i++)
	{
		Compatibilite c = GetCompatibilites().at(i);
		TrueLoveClient c1 = c.GetClient1(), c2 = c.GetClient2();
		listWidget->addItem(QString("%1 %2 & %3 %4: %5\%")
			.arg(c1.GetPrenom()).arg(c1.GetNom())
			.arg(c2.GetPrenom()).arg(c2.GetNom())
			.arg(c.GetTauxCompatibilite()));
		
	}
}

void RechercheCompatibilite::closeEvent(QCloseEvent *event)
{
	retourAccueil();
}

bool RechercheCompatibilite::ExistsRelation(TrueLoveClient c1, TrueLoveClient c2)
{
	QList<Relation> relations = Relation::GetRelations();
	for(int j=0; j < relations.size(); j++)
	{
		Relation r = relations.at(j);
		if((r.GetId1() == c1.GetId() || r.GetId2() == c2.GetId()) 
			|| (r.GetId1() == c2.GetId() || r.GetId2() == c1.GetId()))
		       return true;	
	}
	
	return false;
}

bool RechercheCompatibilite::ExistsRendezVous(TrueLoveClient c1, TrueLoveClient c2)
{
	QList<RendezVous> rdvs = RendezVous::GetRendezVous();
	for(int j=0; j < rdvs.size(); j++)
	{
		RendezVous r = rdvs.at(j);
		Compatibilite c = r.GetCompatibilite();
		TrueLoveClient cRdv1 = c.GetClient1(), cRdv2 = c.GetClient2();
		
		if((cRdv1.GetId() == c1.GetId() && cRdv2.GetId() == c2.GetId()) 
			|| (cRdv1.GetId() == c2.GetId() && cRdv2.GetId() == c1.GetId()))
		       return true;	
	}
	
	return false;
}

void RechercheCompatibilite::InitCompatibilites()
{
	compatibilites.clear();
	QList<TrueLoveClient> clients = TrueLoveClient::GetClients();
	for(int i=0; i<clients.size(); i++)
	{
		TrueLoveClient c = clients.at(i);
		for(int j=0; j<clients.size(); j++)
		{
			TrueLoveClient cc = clients.at(j);
			if(c.GetId() != cc.GetId())	
			{
				int taux = Compatibilite::CalculTauxCompatibilite(c, cc);
				if(taux > 0 && !ContainsCompatibilite(c, cc))
				{
					Compatibilite comp(c, cc);
					comp.SetTauxCompatibilite(taux);
					if(!ExistsRendezVous(c, cc) && !ExistsRelation(c, cc))
						compatibilites.append(comp);
				}
			}
		}
	}	
}

bool RechercheCompatibilite::ContainsCompatibilite(TrueLoveClient c, TrueLoveClient cc)
{
	for(int i=0; i<compatibilites.size(); i++)
	{
		Compatibilite comp = compatibilites.at(i);
		TrueLoveClient t1 = comp.GetClient1();
		TrueLoveClient t2 = comp.GetClient2();

		if((t1.GetId() == c.GetId() && t2.GetId() == cc.GetId())
			|| (t2.GetId() == c.GetId() && t1.GetId() == cc.GetId()))
			return true;
	}

	return false;
}

void RechercheCompatibilite::makeRDV()
{
	int idx = listWidget->currentRow();
	if(idx < 0)
		return;

	Compatibilite c = compatibilites.at(idx);
	RendezVous rdv(c);
	TrueLoveClient t1 = c.GetClient1();
	TrueLoveClient t2 = c.GetClient2();

	QList<RendezVous> rdvs = RendezVous::GetRendezVous();
	QList<StatistiquesClient> stats = StatistiquesClient::GetStatistiquesClients();

	QFile* file = new QFile("xml/meetings.xml");
	QXmlStreamWriter* writer;
	if(file->open(QIODevice::WriteOnly | QIODevice::Text))
	{
		writer = new QXmlStreamWriter(file);
		writer->setAutoFormatting(true);
		
		writer->writeStartDocument();
		writer->writeStartElement("rdvs");

		for(int i=0; i<rdvs.size(); i++)
			AddRdvXmlProperties(writer, rdvs.at(i));

		AddRdvXmlProperties(writer, rdv);

		writer->writeEndElement();
		writer->writeEndDocument();
		file->close();
	}

	bool t1Found = false, t2Found = false;
	
	for(int i=0; i<stats.size(); i++)
	{
		StatistiquesClient s =  stats.at(i);
		
		if(s.GetIdClient() == t1.GetId())
		{
			t1Found = true;
			s.SetNbRendezVousEnAttente(s.GetNbRendezVousEnAttente() + 1);	
			s.SetNbTotalRendezVous(s.GetNbTotalRendezVous() + 1);
			stats.replace(i, s);
		}
		else if(s.GetIdClient() == t2.GetId())
		{
			t2Found = true;
			s.SetNbRendezVousEnAttente(s.GetNbRendezVousEnAttente() + 1);	
			s.SetNbTotalRendezVous(s.GetNbTotalRendezVous() + 1);
			stats.replace(i, s);
		}
	}

	if(!t1Found)
	{
		StatistiquesClient s(t1.GetId());
		s.SetNbRendezVousEnAttente(1);	
		s.SetNbTotalRendezVous(1);
		stats.append(s);
	}
	
	if(!t2Found)
	{
		StatistiquesClient s(t2.GetId());
		s.SetNbRendezVousEnAttente(1);	
		s.SetNbTotalRendezVous(1);
		stats.append(s);
	}

	StatistiquesClient::SaveStats(stats);

	QMessageBox::information(this, "TrueLove | Rendez-vous pris...", 
		QString("<i>Un rendez-vous a &eacute;t&eacute; programm&eacute; pour %1 et %2. Vous pourrez saisir le compte-rendu de ce rendez-vous via l'&eacute;cran de saisie de compte-rendu de rendez-vous.</i>")
		.arg(t1.GetPrenom()).arg(t2.GetPrenom()));
	
	if(listWidget->count() == 1)
	{
		QMessageBox::information(this, "TrueLove | Fin des rendez-vous", "<i>Un rendez-vous a &eacute;t&eacute; pris pour chaque compatibilit&eacute; amoureuse. Vous allez &ecirc;tre redirig&eacute; vers l'&eacute;cran principal.");
		retourAccueil();
	}
	else
	{
		listWidget->takeItem(idx);
		InitCompatibilites();
	}
}

void RechercheCompatibilite::SaveRendezVous(QList<RendezVous> rdvs)
{
	QFile* file = new QFile("xml/meetings.xml");

	if(file->open(QIODevice::WriteOnly | QIODevice::Text))
	{
		QXmlStreamWriter* writer = new QXmlStreamWriter(file);
		writer->setAutoFormatting(true);
		
		writer->writeStartDocument();

		if(rdvs.size() > 0)
		{
			writer->writeStartElement("rdvs");

			for(int i=0; i<rdvs.size(); i++)
				AddRdvXmlProperties(writer, rdvs.at(i));
		
			writer->writeEndElement();
		}

		writer->writeEndDocument();
		file->close();
	}
}

void RechercheCompatibilite::AddRdvXmlProperties(QXmlStreamWriter* writer, RendezVous rdv)
{
	writer->writeStartElement("rdv");
	writer->writeTextElement("tauxCompatibilite", 
			QString("%1").arg(rdv.GetCompatibilite().GetTauxCompatibilite()));

	writer->writeStartElement("client1");
	NouveauClient::AddClientXmlProperties(writer, rdv.GetCompatibilite().GetClient1());
	writer->writeEndElement();

	writer->writeStartElement("client2");
	NouveauClient::AddClientXmlProperties(writer, rdv.GetCompatibilite().GetClient2());
	writer->writeEndElement();

	writer->writeTextElement("apparenceTo1", QString("%1").arg(rdv.GetApparenceTo1()));
	writer->writeTextElement("charmeTo1", QString("%1").arg(rdv.GetApparenceTo1()));
	writer->writeTextElement("humourTo1", QString("%1").arg(rdv.GetApparenceTo1()));
	writer->writeTextElement("intelligenceTo1", QString("%1").arg(rdv.GetApparenceTo1()));
	writer->writeTextElement("conversationTo1", QString("%1").arg(rdv.GetApparenceTo1()));
	writer->writeTextElement("gentillesseTo1", QString("%1").arg(rdv.GetApparenceTo1()));

	writer->writeTextElement("apparenceTo2", QString("%1").arg(rdv.GetApparenceTo2()));
	writer->writeTextElement("charmeTo2", QString("%1").arg(rdv.GetApparenceTo2()));
	writer->writeTextElement("humourTo2", QString("%1").arg(rdv.GetApparenceTo2()));
	writer->writeTextElement("intelligenceTo2", QString("%1").arg(rdv.GetApparenceTo2()));
	writer->writeTextElement("conversationTo2", QString("%1").arg(rdv.GetApparenceTo2()));
	writer->writeTextElement("gentillesseTo2", QString("%1").arg(rdv.GetApparenceTo2()));

	writer->writeEndElement();
}

void RechercheCompatibilite::updateStats(int idx)
{
	Compatibilite c = compatibilites.at(idx);
	lcdNumberCorrespondanceHobbys->display(c.GetCorrespondanceHobbys()*100/40);
	lcdNumberCorrespondanceCriteres->display(c.GetCorrespondanceCriteres()*100/60);
	lcdNumberDiffAge->display(c.GetDiffAge());
}

void RechercheCompatibilite::retourAccueil()
{
	hide();
	parentWidget()->show();
}

